﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using VISS.MailImpl;
using NCT.Framework;
using NCT.Mail;

namespace SendNotificationForUser {
    class Program {
        static void Main(string[] args) {
            SendNotificationMailToUser();
        }

        static void SendNotificationMailToUser() {
            StreamReader reader = new StreamReader("userlist.txt");
            string line;
            File.AppendAllText("logVerified.txt",  "------------------------" + DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss") + " Begin Send Mail To User--------------------------------------");
            int count = 0;
            while ((line = reader.ReadLine()) != null) {
                string[] items = line.Split('\t');
                string userKey = items[0];
                string userName = items[2];
                string email = items[1];
                int status = Convert.ToInt16(items[3]);
                if (status == 9) {
                    MailEnt.MailVerifiedNotification ent = new MailEnt.MailVerifiedNotification();
                    ent.Subject = "VISS - Thông báo từ VISS: Tài khoản đã được duyệt";
                    ent.Email = email;
                    ent.FullName = userName;
                    ent.UserKey = userKey;
                    MailEnt.MailInfo mailInfo = new MailEnt.MailInfo();
                    mailInfo.MailContent = SerializerUtils.JsonSerialize(ent);
                    mailInfo.SendMailType = ESendMailType.VerifiedNotification;

                    try {
                        if (NCT.Mail.MailQueuePD.CreateInstance().AddToQueue("frontend.viss.vn", SerializerUtils.JsonSerialize(mailInfo)) <= 0) {
                            File.AppendAllText("D:\\logVerified.txt", "Fail add to queue for user: " + userKey);
                        }
                    }
                    catch (Exception ex) {
                        File.AppendAllText("D:\\logVerified.txt", "Fail add to queue for user: " + userKey);
                    }
                }
                else {
                    MailEnt.MailUnVerifyNotification ent = new MailEnt.MailUnVerifyNotification();
                    ent.Subject = "VISS - Thông báo từ VISS: Tài khoản chưa được duyệt";
                    ent.Email = email;
                    ent.FullName = userName;
                    ent.UserKey = userKey;
                    MailEnt.MailInfo mailInfo = new MailEnt.MailInfo();
                    mailInfo.MailContent = SerializerUtils.JsonSerialize(ent);
                    mailInfo.SendMailType = ESendMailType.UnVerifyNotification;

                    try {
                        if (NCT.Mail.MailQueuePD.CreateInstance().AddToQueue("frontend.viss.vn", SerializerUtils.JsonSerialize(mailInfo)) <= 0) {
                            File.AppendAllText("D:\\logVerified.txt", "Fail add to queue for user: " + userKey);
                        }
                    }
                    catch (Exception ex) {
                        File.AppendAllText("D:\\logVerified.txt", "Fail add to queue for user: " + userKey);
                    }
                }
                Console.WriteLine(count++);
            }
            File.AppendAllText("logVerified.txt", "----------------------------End Send Mail To User---------------------------\r\n");
        }
    }
}
